﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DMS.Base;
using DMS.Business.Utility;
using DMS.Business.Modules.Organization;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Web;
using DMS.Business.BaseClass;

namespace DMS.Business.Utility
{
    public class LogHelper : BaseService
    {
        #region 异常处理
        /// <summary>
        /// 记录异常
        /// </summary>
        /// <param name="ex">异常</param>
        /// <returns></returns>
        public static string LogException(Exception ex)
        {
            return LogException(ex, string.Empty);
        }
        /// <summary>
        /// 记录异常
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="msg">错误信息</param>
        /// <returns></returns>
        public static string LogException(Exception ex, string msg)
        {
            try
            {
                string operatorID = string.Empty;
                string operatorName = string.Empty;
                AuthenticationUser user = SystemContext.Current.LoginAuthUser;
                if (user != null)
                {
                    operatorID = user.ID.ToString();
                    operatorName = user.Name;
                }

                return LogManager.LogException(operatorID, operatorName, ex, msg);
            }
            catch (Exception ex1)
            {
                return ex1.Message;
            }
        }

        /// <summary>
        /// 记录异常（没有当前登录用户）
        /// </summary>
        public static string LogException2(Exception ex)
        {
            return LogManager.LogException("", "", ex, "");
        }
        /// <summary>
        /// 保存异常信息
        /// </summary>
        /// <param name="limite"></param>
        /// <param name="page"></param>
        /// <param name="total"></param>
        /// <param name="condition"></param>
        /// <param name="dbParams"></param>
        /// <returns></returns>
        public static DataSet GetLogs(int limite, int page, out int total, string condition, List<DbParameter> dbParams)
        {
            total = 0;
            return LogManager.GetLogs(limite, page, out total, condition, dbParams);
        }
        #endregion

        #region 操作日志
        /// <summary>
        /// 写用户登录日志
        /// </summary>
        /// <param name="user">用户</param>
        /// <returns></returns>
        public static string LogUserLogin(AuthenticationUser user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user", "写用户登录日志，传入的user参数不能为空");
            }

            string msg = string.Format("用户：{0}({1})于{2}登录系统", user.Name, user.LoginID, DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
            LogUserAction(user, "用户登录", msg);
            return msg;
        }
        /// <summary>
        /// 用户退出系统
        /// </summary>
        /// <param name="user"></param>
        public static void LogUserLogout(AuthenticationUser user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user", "写用户登录日志，传入的user参数不能为空");
            }

            string msg = string.Format("用户：{0}({1})于{2}退出系统", user.Name, user.LoginID, DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
            LogUserAction(user, "退出系统", msg);
        }
        /// <summary>
        /// 记录当前用户操作
        /// </summary>
        /// <param name="actionCode"></param>
        /// <param name="actionContent"></param>
        public static void LogAction(string actionCode, string actionContent)
        {
            AuthenticationUser user = SystemContext.Current.LoginAuthUser;
            LogUserAction(user, actionCode, actionContent);
        }

        /// <summary>
        /// 记录当前用户操作（没有当前登录用户）
        /// </summary>
        /// <param name="actionCode"></param>
        /// <param name="actionContent"></param>
        public static void LogAction2(string actionCode, string actionContent)
        {
            LogUserAction(null, actionCode, actionContent);
        }

        /// <summary>
        /// 记录用户操作
        /// </summary>
        /// <param name="actor"></param>
        /// <param name="actionCode"></param>
        /// <param name="actionContent"></param>
        public static void LogUserAction(AuthenticationUser actor, string actionCode, string actionContent)
        {
            string operatorID = string.Empty;
            string operatorName = string.Empty;
            if (actor != null)
            {
                operatorID = actor.ID.ToString();
                operatorName = actor.Name;
            }
            LogManager.SaveAction(operatorID, operatorName, actionCode, actionContent);
        }

        /// <summary>
        /// 获取操作类型
        /// </summary>
        /// <returns></returns>
        public static DataSet GetActionCode()
        {
            return LogManager.GetActionCode();
        }
        /// <summary>
        /// 获取操作列表
        /// </summary>
        /// <param name="limite"></param>
        /// <param name="page"></param>
        /// <param name="total"></param>
        /// <param name="condition"></param>
        /// <param name="dbParams"></param>
        /// <returns></returns>
        public static DataSet GetActions(int limite, int page, out int total, string condition, List<DbParameter> dbParams)
        {
            total = 0;
            return LogManager.GetActions(limite, page, out total, condition, dbParams);
        }

        #endregion
    }
}
